#include<iostream>
using namespace std;
 #include <sys/types.h>          /* See NOTES */
 #include <sys/socket.h>
#include<unistd.h>
#include<cstring>
#include<arpa/inet.h>
#include <ctype.h>
#define PORT 8888
#define BUFFER_SIZE 64
#include"stdShared.h"
#include<pthread.h>
#include<json-c/json.h>
struct UserInfo
{
    UserInfo()
    {

    }
     UserInfo(int type,char *name,char *passwd)
     {
        m_type=type;
        strcpy(m_name,name);
        strcpy(m_passwd,passwd);
        
     };
    int m_type;
    char m_name[BUFFER_SIZE];
    char m_passwd[BUFFER_SIZE];

};
/*线程函数*/
void *server_func(void *arg)
{
        /*线程分离*/
        pthread_detach(pthread_self());

        int connfd=*((int*)arg);
         //读
       char buffer[BUFFER_SIZE]={0};
        // int quest=0;
        while(1){
            int readBytes=read(connfd,buffer,sizeof(buffer));
            if(readBytes>0)
            {
                cout<<"buffer:"<<buffer<<endl;
                json_object* jsonObj=json_tokener_parse(buffer);
                if(jsonObj==NULL)
                {
                    cout<<"解析失败"<<endl;
                }
                else
                {
                int type=json_object_get_int(json_object_object_get(jsonObj,"type"));
                const char *username=json_object_get_string(json_object_object_get(jsonObj,"username"));
                const char *passwd=json_object_get_string(json_object_object_get(jsonObj,"passwd"));
                cout <<"type:"<<type<<"username:"<<username<<"passwd:"<<passwd<<endl;
                
                }         
            // int type=info.m_type;
            // if(type==REGISTER)
            // {
            //     cout<<"欢迎注册"<<endl;
            //     cout<<"name:"<<info.m_name<<"  passwd:"<<info.m_passwd<<endl;
            // }
            // else if(type==LOGIN)
            // {
            //     cout<<"欢迎登陆"<<endl;
            //     cout<<"username:"<<info.m_name<<",passwd:"<<info.m_passwd<<endl;
            // }
            }
            else if(readBytes==0)
            {
                /*客户端下线*/
                cout<<"客户端下线"<<endl;
                break;
            }
            else
            {
                cout<<"readByte<0"<<endl;
            }
            sleep(10);
            close(connfd);
            /*线程退出*/
            pthread_exit(NULL);

 }
}
int main()
{
    /*创建通信文件描述符*/
    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    if(sockfd==-1)
    {
        perror("create error:");
        _exit(-1);

    }
    //设置地址复用
    int optval=1;//1开启
    int ret = setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval));
    if(ret==-1){
        perror("set opt error:");
        _exit(-1);
    }

    /*绑定服务器的ip和端口信息*/
    sockaddr_in localAddress;
    /*清除脏数据*/
    memset(&localAddress,0,sizeof(localAddress));
    /*地址族*///IPV4
    localAddress.sin_family=AF_INET;  
    //端口
    localAddress.sin_port=htons(PORT);
    localAddress.sin_addr.s_addr=INADDR_ANY;
    ret=bind(sockfd,(const sockaddr*)&localAddress,sizeof(localAddress));
    if(ret==-1)
    {
        perror("bind error");
        _exit(-1);
    }
    //监听************
    ret=listen(sockfd,10);
    if(ret==-1){
        perror("listen error:");
        _exit(-1);
    }
    cout<<"等待客户端连接..."<<endl;
    //阻塞接受客户端的连接
    while(1)
    {    
    
        int connfd=accept(sockfd,NULL,NULL);
        if(connfd==-1)
        {
            perror("accept error:");
            _exit(-1);
        }
        cout<<"connfd:"<<connfd<<endl;

        /*创建线程*/
        pthread_t tid;
         ret=pthread_create(&tid,NULL,server_func,(void *)&connfd);
         if(ret!=0)
        {
         perror("thread create error");
         exit(-1);
        }

        /*手动休眠1s*/
        sleep(1);
    }
       
   
        
        // for(int i=0;i<readBytes;i++){
        //     buf[i]=toupper(buf[i]);
        // }
        // write(connfd,buf,readBytes);
        //memset(&info,0,sizeof(info));
        // read(connfd,&quest,sizeof(int));
        // if(quest){
        //     write(connfd,&info,sizeof(info));
        //     cout<<"name:"<<info.m_name<<"  passwd:"<<info.m_passwd<<endl;
        //     quest=0;
        // }

        

    close(sockfd);



    return  0;
}